local hx__ximing = fk.CreateSkill {
  name = "hx__ximing",
  tags = { Skill.Compulsory, },
}

local updataXianfu = function (room, player, target)
  local mark = player:getTableMark("hx__ximing")
  table.insertIfNeed(mark[2], target.id)
  room:setPlayerMark(player, "hx__ximing", mark)
  local names = table.map(mark[2], function(pid) return Fk:translate(room:getPlayerById(pid).general) end)
  room:setPlayerMark(player, "@hx__ximing", table.concat(names, ","))
end

hx__ximing:addEffect(fk.GameStart, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(hx__ximing.name) and #player.room:getOtherPlayers(player, false) > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:notifySkillInvoked(player, hx__ximing.name)
    player:broadcastSkillInvoke(hx__ximing.name, math.random(2))
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player, false),
      prompt = "#hx__ximing-choose",
      skill_name = hx__ximing.name,
      cancelable = false,
      no_indicate = true,
    })[1]
    local mark = player:getTableMark(hx__ximing.name)
    if #mark == 0 then mark = {{},{}} end
    table.insertIfNeed(mark[1], to.id)
    room:setPlayerMark(player, hx__ximing.name, mark)
  end,
})

hx__ximing:addEffect(fk.Damaged, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("hx__ximing")
    return not player.dead and not target.dead and #mark > 0 and table.contains(mark[1], target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    updataXianfu (room, player, target)
    room:sendLog{ type = "#SkillDelayInvoked", from = player.id, arg = "hx__ximing", }
    player:broadcastSkillInvoke(hx__ximing.name, math.random(2)+2)
    room:notifySkillInvoked(player, hx__ximing.name, "negative")
    room:damage{
      to = player,
      damage = data.damage,
      skillName = hx__ximing.name,
    }
  end,
})
hx__ximing:addEffect(fk.HpRecover, {
  mute = true,
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("hx__ximing")
    return not player.dead and not target.dead and #mark > 0 and table.contains(mark[1], target.id) and player:isWounded()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(hx__ximing.name, math.random(2)+4)
    room:notifySkillInvoked(player, hx__ximing.name, "support")
    room:recover{
      who = player,
      num = data.num,
      recoverBy = player,
      skillName = hx__ximing.name,
    }
  end,
})
hx__ximing:addEffect(fk.EnterDying, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local mark = player:getTableMark("hx__ximing")
    if event == fk.EnterDying and player:hasSkill("hx__ximing") and target.hp < 1 then
      return #mark > 0 and table.contains(mark[1], target.id)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EnterDying then
      room:notifySkillInvoked(player, "hx__ximing","defensive")
      player:setSkillUseHistory("hx__jianjia", 0, Player.HistoryGame)
    end
  end,
})

return hx__ximing